package com.mq.test.config.mq;


//import com.platform.test.config.mq.listener.MQConsumeListener;

import com.mq.test.config.mq.listener.MQConsumeListener;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MQConfig {

   // 实现channelawaremesslistener接口方式
   @Autowired
   MQConsumeListener mqConsumeListener;
    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer(CachingConnectionFactory cachingConnectionFactory){

        SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
        Runnable a = simpleMessageListenerContainer::getActiveConsumerCount;
        simpleMessageListenerContainer.setQueueNames("queue.name");
        simpleMessageListenerContainer.setConnectionFactory(cachingConnectionFactory);
        simpleMessageListenerContainer.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        simpleMessageListenerContainer.setMessageListener(mqConsumeListener);
        return simpleMessageListenerContainer;
    }

    @Bean
    public Queue testQueue(){
        return new Queue(QueueName.TEST,true);
    }
    @Bean
    public DirectExchange testDirectExchange(){
        return new DirectExchange(ExchangeName.TEST,true,false);
    }
    @Bean
    public Binding testBinding(){
        return BindingBuilder.bind(testQueue()).to(testDirectExchange()).with(RoutingKey.TEST);
    }
    static class QueueName{
        static String TEST = "queue.test";
    }
    static class ExchangeName{
        static String TEST = "exchange.test";
    }

    enum RoutingKey{
        TEST("exchange.test");
        private String key;
        RoutingKey(String key){
            this.key = key;
        }
        @Override
        public String toString(){
            return this.key;
        }
    }
}


